package com.mallcai.bigdata.ladon.utils.excel;

import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

/**
 * described : 报表样式统一写在这里,用的时候统一从这里获取
 * Created by oneape on 2017-10-25 14:38.
 * Modify:
 */
public class CustomCellStyle {
    /**
     * 标题样式关键字
     */
    public static final String STYLE_TITLE  = "TitleStyle";
    /**
     * 表头样式关键字
     */
    public static final String STYLE_HEADER = "HeaderStyle";
    /**
     * 单元格样式关键字
     */
    public static final String STYLE_CELL   = "CellStyle";
    /**
     * 提示样式关键字
     */
    public static final String STYLE_TIP    = "TipStyle";
    /**
     * 合计样式关键字
     */
    public static final String STYLE_SUM    = "SumStyle";

    /**
     * 头部样式
     *
     * @param book HSSFWorkbook
     */
    public static CellStyle initHeaderStyle(HSSFWorkbook book) {
        CellStyle style = book.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);   //水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        HSSFColor lightGreen = setColor(book, (byte) 0x80, (byte) 0xB9, (byte) 0x7F);//设置为绿色
        style.setFillForegroundColor(lightGreen.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        //设置字体
        Font font = book.createFont();
        font.setFontHeightInPoints((short) 12);     //字号：12号
        font.setBold(true);   //变粗
        style.setFont(font);
        return style;
    }

    /**
     * 合计样式
     *
     * @param book HSSFWorkbook
     */
    public static CellStyle initSumStyle(HSSFWorkbook book) {
        CellStyle style = book.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);   //水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
//        HSSFColor lightGreen = setColor(book, (byte) 0xFF, (byte) 0xD7, (byte) 0x00);//设置为黄色
        style.setFillForegroundColor(HSSFColor.YELLOW.index);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        //设置字体
        Font font = book.createFont();
        font.setFontHeightInPoints((short) 11);     //字号：11号
        font.setBold(true);   //变粗
        style.setFont(font);
        return style;
    }

    /**
     * 单元格样式
     *
     * @param book HSSFWorkbook
     */
    public static CellStyle initCellStyle(HSSFWorkbook book) {
        CellStyle style = book.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);   //水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        style.setWrapText(true);
        //设置字体
        Font font = book.createFont();
        font.setFontHeightInPoints((short) 12);     //字号：12号
        style.setFont(font);
        return style;
    }

    /**
     * 标题样式
     *
     * @param book HSSFWorkbook
     */
    public static CellStyle initTitleStyle(HSSFWorkbook book) {
        CellStyle style = book.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);   //水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        //设置字体
        Font font = book.createFont();
        font.setFontHeightInPoints((short) 18);     //字号：18号
        font.setBold(true);   //变粗
        style.setFont(font);
        return style;
    }

    /**
     * 提示信息样式
     *
     * @param book HSSFWorkbook
     */
    public static CellStyle initTipStyle(HSSFWorkbook book) {
        CellStyle style = book.createCellStyle();
        style.setAlignment(HorizontalAlignment.LEFT);   //水平居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        style.setWrapText(true);
        //设置字体
        Font font = book.createFont();
        font.setFontHeightInPoints((short) 14);     //字号：14号
        style.setFont(font);
        return style;
    }

    /**
     * 设置自定义颜色
     *
     * @param workbook HSSFWorkbook
     * @param r        byte
     * @param g        byte
     * @param b        byte
     * @return HSSFColor
     */
    public static HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b) {
        HSSFPalette palette = workbook.getCustomPalette();
        HSSFColor hssfColor = null;
        try {
            hssfColor = palette.findColor(r, g, b);
            if (hssfColor == null) {
                palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g, b);
                hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return hssfColor;
    }
}
